package com.txhy.certificatechain.ui.activity.welcome;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ninetripods.aopermission.permissionlib.annotation.NeedPermission;
import com.txhy.certificatechain.R;
import com.txhy.certificatechain.ui.adapter.GuidePageAdapter;
import com.txhy.certificatechain.utils.PrefUtil;

import java.util.ArrayList;
import java.util.List;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2014-2016, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: WelcomeActivity.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述: 欢迎页面
 * 创建时间: 2018/9/18 16:26
 * 更新时间：2018/9/18 16:26
 * -----------------------------------------------------------------
 * 2018/9/18 16:26 : 创建 WelcomeActivity.java (侯建军);
 * -----------------------------------------------------------------
 */
public class WelcomeActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    //浏览页
    ViewPager vpWelcome;
    //图像视图
    ImageView ivRegister;
    //视图组
    ViewGroup viewGroup;
    // guide_ib_start  guide_ll_point
    private int[] imageIdArray;//图片资源的数组
    private List<View> viewList;//图片资源的集合
    //实例化原点View
    private ImageView ivPoint;
    private ImageView[] ivPointArray;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置无标题栏
//        getSupportActionBar().hide();
        //设置为无标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置为全屏模式
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome);
        //加载底部圆点
        vpWelcome = findViewById(R.id.vpWelcome);
        ivRegister = findViewById(R.id.ivRegister);
        ivRegister.setOnClickListener(this);
        viewGroup = findViewById(R.id.guide_ll_point);
        //设置引导页
        setViewPager();
        //初始化点
        initPoint();
        //初始化电话按键
//        initPhonePress();
    }
    @NeedPermission(value = {android.Manifest.permission.READ_PHONE_STATE}, requestCode = 0)
    private void initPhonePress() {
    }
    /**
     * 初始化圆点
     */
    private void initPoint() {
        //根据ViewPager的item数量实例化数组
        ivPointArray = new ImageView[viewList.size()];
        //根据ViewPager的item数量实例化数组
        //循环新建底部圆点ImageView，将生成的ImageView保存到数组中
        int size = viewList.size();
        for (int i = 0; i < size; i++) {
            ivPoint = new ImageView(this);
            ivPoint.setLayoutParams(new ViewGroup.LayoutParams(20, 20));
            ivPoint.setPadding(30, 0, 30, 0);//left,top,right,bottom
            ivPointArray[i] = ivPoint;
            //第一个页面需要设置为选中状态，这里采用两张不同的图片
            if (i == 0) {
                ivPoint.setBackgroundResource(R.mipmap.douodouhei);
            } else {
                ivPoint.setBackgroundResource(R.mipmap.douodubai);
            }
            //将数组中的ImageView加入到ViewGroup
            viewGroup.addView(ivPointArray[i]);
        }
    }
    /**
     * 设置浏览页
     */
    private void setViewPager() {
        //实例化图片资源
        imageIdArray = new int[]{R.mipmap.welcome1, R.mipmap.welcome2, R.mipmap.welcome3, R.mipmap.welcome4};
        viewList = new ArrayList<>();
        //获取一个Layout参数，设置为全屏
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

        //循环创建View并加入到集合中
        int len = imageIdArray.length;
        for (int i = 0; i < len; i++) {
            //new ImageView并设置全屏和图片资源
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(params);
            imageView.setBackgroundResource(imageIdArray[i]);

            //将ImageView加入到集合中
            viewList.add(imageView);
        }

        //View集合初始化好后，设置Adapter
        vpWelcome.setAdapter(new GuidePageAdapter(viewList));
        //设置滑动监听
        vpWelcome.addOnPageChangeListener(WelcomeActivity.this);
    }
    /**
     *  页面滚动
     * @param position
     * @param positionOffset
     * @param positionOffsetPixels
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    /**
     * 滑动后的监听
     * @param position
     */
    @Override
    public void onPageSelected(int position) {
        //循环设置当前页的标记图
        int length = imageIdArray.length;
        for (int i = 0; i < length; i++) {
            ivPointArray[position].setBackgroundResource(R.mipmap.ic_launcher);
            if (position != i) {
                ivPointArray[i].setBackgroundResource(R.mipmap.ic_launcher);
            }
        }
        //判断是否是最后一页，若是则显示按钮
        if (position == imageIdArray.length - 1) {
            ivRegister.setVisibility(View.VISIBLE);
        } else {
            ivRegister.setVisibility(View.GONE);
        }
    }

    /**
     * 页面状态改变
     * @param state
     */
    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 点击开启时代按钮
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivRegister:
                //打开闪屏
                startActivitySplashActivity();
                break;
        }
    }

    /**
     * 打开闪屏
     */
    @NeedPermission(value = {android.Manifest.permission.READ_PHONE_STATE}, requestCode = 0)
    private void startActivitySplashActivity(){
        startActivity(new Intent(WelcomeActivity.this, SplashActivity.class));
        PrefUtil.setWelcomeBoolean(WelcomeActivity.this, true);
        finish();
    }
}
